package sy.controller;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sy.jackson.SysProjectMixin;
import sy.jackson.SysUnitMixin;
import sy.util.MessageObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import sy.model.SysBuilding;
import sy.model.SysDivision;
import sy.model.SysProject;
import sy.model.SysUnit;
import sy.service.SysBuildingServiceI;
import sy.service.SysDivisionServiceI;
import sy.service.SysProjectServiceI;
import sy.service.SysUnitServiceI;
/**
 * @Description: 首页
 * @param 
 * @author     :wjy
 * Create Date : 2017年7月26日 
 */
@Controller
@SessionAttributes("currAccount")
@RequestMapping("unitController")
public class UnitController {
	
	@Autowired
	private SysUnitServiceI sysUnitService;
	@Autowired
	private SysBuildingServiceI sysBuildingService;
	@Autowired
	private SysProjectServiceI sysProjectService;
	
	private static final ObjectMapper objectMapper = new ObjectMapper();
	

	/************************** view controller ***************************/
	
	/**
	 * @Description: 首页视图
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月30日 
	 */
	@RequestMapping(value="/view")
	public String objectView(Model model){
		List<SysUnit> unitList = sysUnitService.list();
		
		model.addAttribute("unitList",unitList);
		return "addressInfo/unit/unit";
	}
	
	/**
	 * @Description: 新增视图
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月30日 
	 */
	@RequestMapping(value="/view/new",method=RequestMethod.GET)
	public String newObjectInfo(Model model){
		
		String uuid = UUID.randomUUID().toString();
		SysUnit unit = new SysUnit();
		unit.setId(uuid);
		
		List<SysBuilding> buildingList = sysBuildingService.list();
		List<SysProject> projectList = sysProjectService.list();

		model.addAttribute("unit",unit);
		model.addAttribute("buildingList",buildingList);
		model.addAttribute("projectList",projectList);
		model.addAttribute("type",1);
		
		return "addressInfo/unit/unitFrom";
	}

	/**
	 * @Description: 编辑视图
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月30日 
	 */
	@RequestMapping(value="/view/edit",params="id",method=RequestMethod.GET)
	public String editObjectInfo(@RequestParam String id,Model model){
		
		SysUnit unit = sysUnitService.getSysUnitById(id);
		List<SysBuilding> buildingList = sysBuildingService.list();
		List<SysProject> projectList = sysProjectService.list();

		model.addAttribute("unit",unit);
		model.addAttribute("buildingList",buildingList);
		model.addAttribute("projectList",projectList);
		model.addAttribute("type",2);
		
		return "addressInfo/unit/unitFrom";
	}
		
	/************************** data controller ***************************/
	
	/**
	 * @Description: 增加
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月30日 
	 */
	@ResponseBody
	@RequestMapping(value = "/new", method = RequestMethod.POST)
	private MessageObject newObjectInfo( @RequestBody SysUnit unit, Model model) {
		MessageObject messageObject = new MessageObject();
		try {
			sysUnitService.save(unit);
			messageObject.setMessage("添加成功 !");
			messageObject.setStatus(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageObject.setMessage("添加失败 !");
			messageObject.setStatus(false);
		}
		
		return messageObject;
	}
	
	/**
	 * @Description: 编辑
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月30日 
	 */
	@ResponseBody
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	private MessageObject editObjectInfo( @RequestBody SysUnit unit, Model model) {
		MessageObject messageObject = new MessageObject();
		try {
			sysUnitService.update(unit);
			messageObject.setMessage("编辑成功 !");
			messageObject.setStatus(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageObject.setMessage("编辑失败 !");
			messageObject.setStatus(false);
		}
		
		return messageObject;
	}
	
	/**
	 * @Description: 删除
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月26日 
	 */
	@RequestMapping(value="/del", method=RequestMethod.POST)
	@ResponseBody
	private MessageObject delObjectInfo(@RequestParam("id") String[] ids) {
		MessageObject messageObject = new MessageObject();
		
		try {
			sysUnitService.remove(ids);
			messageObject.setMessage("删除成功!");
			messageObject.setStatus(true);
		} catch (Exception e) {
			// TODO: handle exception
			messageObject.setMessage("删除失败!");
			messageObject.setStatus(false);
		}
		return messageObject;
	}
	
	
	/**
	 * @Description: 返回全部json数据
	 * @param 
	 * @author     :wjy
	 * Create Date : 2017年7月26日 
	 */
	@ResponseBody
	@RequestMapping(value = "/getAllData", method = RequestMethod.POST,produces={"text/html;charset=UTF-8"})
	private String getAllData(HttpServletResponse response) {
		objectMapper.addMixInAnnotations(SysUnit.class, SysUnitMixin.BasicInfo.class);
		List<SysUnit> unitList = sysUnitService.list();
		try {
			return objectMapper.writeValueAsString(unitList);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
}
